<?php

class Insider extends Controller {

	function Insider()
	{
		parent::Controller();	
	}     
    
    function index()
    {            
        if (session_check()) {
            redirect('insider/profile', 'refresh');
        }
               
        //load page header
        $this->load->model('pageheadersmodel'); 
        $data["header"] = $this->pageheadersmodel->get_header('insider');

        $this->load->view('website/default/head', array("document_title" => $this->lang->line('Members_Program')." - You need to register to gain full site access")); 
        $this->load->view('website/default/header'); 
        $this->load->view('website/insider/insider', $data);   
        $this->load->view('website/default/footer');   

    }

	// Method: _validate_profile_form
	// CreatedBy: Jonnas
	// Created: 05/06/2013
	
    private function _validate_profile_form(){

        $this->load->library("form_validation");    		
        
		if(isset($_POST["title"]))
		{
			$config = array(
				array(
					'field'   => 'title',
					'label'   => 'title',
					'rules'   => 'trim|required|xss_clean|max_length[8]'
				)
			);
			
			$this->form_validation->set_rules($config);       
			$this->form_validation->set_error_delimiters('<p class="error">', '</p>');

			if ($this->form_validation->run($this) == FALSE){
				return false;				
			}
			else{
				return true;								
			}						
		}
		if(isset($_POST["forename"]))
		{
			$config = array(
				array(
					'field'   => 'forename',
					'label'   => 'Forename',
					'rules'   => 'trim|required|xss_clean|max_length[100]'
				)
			);
			
			$this->form_validation->set_rules($config);       
			$this->form_validation->set_error_delimiters('<p class="error">', '</p>');

			if ($this->form_validation->run($this) == FALSE){
				return false;				
			}
			else{
				return true;								
			}						
		}
		if(isset($_POST["surname"]))
		{
			$config = array(
				array(
					'field'   => 'surname',
					'label'   => 'Surname',
					'rules'   => 'trim|required|xss_clean|max_length[100]'
				)
			);
			
			$this->form_validation->set_rules($config);       
			$this->form_validation->set_error_delimiters('<p class="error">', '</p>');

			if ($this->form_validation->run($this) == FALSE){
				return false;				
			}
			else{
				return true;								
			}						
		}
		if(isset($_POST["companyname"]))
		{
			$config = array(
				array(
					'field'   => 'companyname',
					'label'   => 'Company Name',
					'rules'   => 'trim|required|xss_clean|max_length[200]'
				)
			);
			
			$this->form_validation->set_rules($config);       
			$this->form_validation->set_error_delimiters('<p class="error">', '</p>');

			if ($this->form_validation->run($this) == FALSE){
				return false;				
			}
			else{
				return true;								
			}						
		}
		if(isset($_POST["clientaddress"]))
		{
			$config = array(
				array(
					'field'   => 'clientaddress',
					'label'   => 'Address',
					'rules'   => 'trim|required|xss_clean|max_length[250]'
				)
			);
			
			$this->form_validation->set_rules($config);       
			$this->form_validation->set_error_delimiters('<p class="error">', '</p>');

			if ($this->form_validation->run($this) == FALSE){
				return false;				
			}
			else{
				return true;								
			}						
		}
		if(isset($_POST["city"]))
		{
			$config = array(
				array(
					'field'   => 'city',
					'label'   => 'City',
					'rules'   => 'trim|xss_clean|required|max_length[100]'
				)
			);
			
			$this->form_validation->set_rules($config);       
			$this->form_validation->set_error_delimiters('<p class="error">', '</p>');

			if ($this->form_validation->run($this) == FALSE){
				return false;				
			}
			else{
				return true;								
			}						
		}
		if(isset($_POST["state_province_county"]))
		{
			$config = array(
				array(
					'field'   => 'state_province_county',
					'label'   => 'State/Province/County',
					'rules'   => 'trim|xss_clean|required|max_length[100]'
				)
			);
			
			$this->form_validation->set_rules($config);       
			$this->form_validation->set_error_delimiters('<p class="error">', '</p>');

			if ($this->form_validation->run($this) == FALSE){
				return false;				
			}
			else{
				return true;								
			}						
		}
		if(isset($_POST["zip"]))
		{
			$config = array(
				array(
					'field'   => 'zip',
					'label'   => 'ZIP/Postal Code',
					'rules'   => 'trim|xss_clean|required|max_length[50]'
				)
			);
			
			$this->form_validation->set_rules($config);       
			$this->form_validation->set_error_delimiters('<p class="error">', '</p>');

			if ($this->form_validation->run($this) == FALSE){
				return false;				
			}
			else{
				return true;								
			}						
		}
		if(isset($_POST["country"]))
		{
			$config = array(
				array(
					'field'   => 'country',
					'label'   => 'Country',
					'rules'   => 'trim|xss_clean|required'
				)
			);
			
			$this->form_validation->set_rules($config);       
			$this->form_validation->set_error_delimiters('<p class="error">', '</p>');

			if ($this->form_validation->run($this) == FALSE){
				return false;				
			}
			else{
				return true;								
			}						
		}
		if(isset($_POST["phonenumber"]))
		{
			$config = array(
				array(
					'field'   => 'phonenumber',
					'label'   => 'Phone Number',
					'rules'   => 'trim|xss_clean|required|max_length[30]'
				)
			);
			
			$this->form_validation->set_rules($config);       
			$this->form_validation->set_error_delimiters('<p class="error">', '</p>');

			if ($this->form_validation->run($this) == FALSE){
				return false;				
			}
			else{
				return true;								
			}						
		}
		if(isset($_POST["mobile"]))
		{
			$config = array(
				array(
					'field'   => 'mobile',
					'label'   => 'Mobile Phone',
					'rules'   => 'trim|xss_clean|required|max_length[30]'
				) 
			);
			
			$this->form_validation->set_rules($config);       
			$this->form_validation->set_error_delimiters('<p class="error">', '</p>');

			if ($this->form_validation->run($this) == FALSE){
				return false;				
			}
			else{
				return true;								
			}						
		}		
		if(isset($_POST["email"]))
		{
			$config = array(
				array(
					'field'   => 'email',
					'label'   => 'Email address',
                    'rules'   => 'trim|xss_clean|required|valid_email|callback__check_duplicated_email'
				),
				array(
				 'field'   => 'email2',
				 'label'   => 'Email Address verification',
				 'rules'   => 'trim|xss_clean|required|matches[email]'
				)				 
			);
			
			$this->form_validation->set_rules($config);       
			$this->form_validation->set_error_delimiters('<p class="error">', '</p>');

			if ($this->form_validation->run($this) == FALSE){
				return false;				
			}
			else{
				return true;								
			}						
		}		

    }
	
	// Method: updateprofile
	// CreatedBy: Jonnas
	// Created: 05/06/2013
	
	function updateprofile()
	{
        if (!session_check()) {
            return false;
        }		

		$this->load->model('usermodel');
		
		if($this->_validate_profile_form())
		{

			$data['user_data'] = array(
				'fieldname' => $this->input->post("fieldname"),
				'fieldvalue' => $this->input->post("fieldvalue"),
				'idusers' => $this->session->userdata('user_id')
			); 
						
			$str = $this->usermodel->update_user_details($data['user_data']);

			if($str)
			{
				$result = array(
							'success' => TRUE
							);
				
				if($this->input->post("fieldname") == 'email')
				{
					$_SESSION['user'] = $this->input->post("fieldvalue");
				}
				
				echo json_encode($result);
			}
			else
			{
				$result = array(
							'success' => FALSE,
							'message' => "Update error"
							);
							
				echo json_encode($result);
			}
									
		}
		else
		{
			//die('AQUI');
			$this->form_validation->set_error_delimiters('', '');

			if(!isset($_POST["email"]))
			{
				$result = array(
							'success' => FALSE,
							'message' => form_error($this->input->post("fieldname"))						
							);
			}
			else
			{
				$result = array(
							'success' => FALSE,
							'message' => form_error($this->input->post("fieldname")) . 
								form_error($this->input->post("fieldname") . "2")
							);
			}
							
			echo json_encode($result);
		}

	}

	// Method: Profile                           
	// CreatedBy: Jonnas                         
	// Created: 04/06/2013                       
	
	function profile()
	{
				
        if (!session_check()) {
            set_referrer();
            redirect('insider', 'refresh');
        }
		
		
        $this->load->library("form_validation");    		
				
        //load page header
        $this->load->model('pageheadersmodel'); 
        $data["header"] = $this->pageheadersmodel->get_header('insider');

		//load user data
		$this->load->model('usermodel');
		$data["userdetails"] = $this->usermodel->get_user_details($this->session->userdata('user_id'));

        $this->load->view('website/default/head', array("document_title" => DOCUMENT_TITLE . " ".$this->lang->line('Members_Program') . " registration")); 
        $this->load->view('website/default/header'); 
        $this->load->view('website/insider/profile', $data);   
        $this->load->view('website/default/footer');   
		
				
		
	}
	
	function _validate_editpassword_form()
	{
		
        $config = array(
						array(
                         'field'   => 'current_password',
                         'label'   => 'Current password',
                         'rules'   => 'required|min_length[4]|max_length[20]'
                        ),
						array(
                         'field'   => 'new_password',
                         'label'   => 'New password',
                         'rules'   => 'required|min_length[4]|max_length[20]'
                        ),
                        array(
                         'field'   => 'new_password_confirm',
                         'label'   => 'Confirm new password',
                         'rules'   => 'required|matches[new_password]'
                        )
					);
        $this->form_validation->set_rules($config);       
        $this->form_validation->set_error_delimiters('<p class="error">', '</p>');
        
        if ($this->form_validation->run($this) == FALSE){
            return FALSE;
        }else
            return TRUE;
					
		
	}
	
    function edit_password()
    {

		$this->load->library("form_validation");

        if (!session_check()) {
            set_referrer();
            redirect('insider/edit_password', 'refresh');
        }		
	
		$this->load->model('usermodel');
	
		$success = FALSE;
	
		$error = FALSE;
        if($this->input->post("recover_submit") !== FALSE){
          			
            if(validate_token("edit_password",$this->input->post("password_token",TRUE))){

                if($this->_validate_editpassword_form()){
				
					if($this->usermodel->check_valid_user_byid($this->session->userdata('user_id'),$this->input->post('current_password')))
					{
							
						$str = $this->usermodel->set_new_password($this->session->userdata('user_id') ,sha1($this->input->post("new_password",TRUE)));
						
						$success = TRUE;
						
					}
					else
					{
						$data['error'] = "<p class='error'>".$this->lang->line('PasswordEditWrongPass')."</p>";
					}
				}
			}
		}

	
		//load page header
		$this->load->model('pageheadersmodel'); 
        $data["header"] = $this->pageheadersmodel->get_header('register');
		
		$data['token'] = generate_token("edit_password",10);	

        $this->load->view('website/default/head', array("document_title" => DOCUMENT_TITLE . "Register as a ".$this->lang->line('Members_Program'))); 
        $this->load->view('website/default/header');
		if($success)
		{
			$this->load->view('website/insider/edit_password_success', $data);
		}
		else
		{
	        $this->load->view('website/insider/edit_password', $data);  
		}
        $this->load->view('website/default/footer');   
	
    }		
	

    function newsletters()
    {
                                   
        //load page header
        $this->load->model('pageheadersmodel'); 
        $data["header"] = $this->pageheadersmodel->get_header('newsletters');

        //newsletter list                                    
        $this->load->model('newslettersmodel');      
        $data["list"] = $this->newslettersmodel->get_list(); 

        $this->load->view('website/default/head', array("document_title" => DOCUMENT_TITLE . "Newsletters")); 
        $this->load->view('website/default/header');
        $this->load->view('website/insider/newsletters', $data);   
        $this->load->view('website/default/footer');   

    }
    
    function register()
    {

        if (session_check()) {
            redirect('insider/profile', 'refresh');
        }

        //load page header
        $this->load->model('pageheadersmodel'); 
        $data["header"] = $this->pageheadersmodel->get_header('register');

        $this->load->view('website/default/head', array("document_title" => DOCUMENT_TITLE . "Please register")); 
        $this->load->view('website/default/header');
        $this->load->view('website/insider/register', $data);  
        $this->load->view('website/default/footer');   

    }
    
    function login()
    {

        if (session_check()) {
            redirect('insider', 'refresh');
        }

        //load page header
        $this->load->model('pageheadersmodel'); 
        $data["header"] = $this->pageheadersmodel->get_header('login');

        $this->load->view('website/default/head', array("document_title" => DOCUMENT_TITLE . "Login")); 
        $this->load->view('website/default/header');
        $this->load->view('website/insider/login', $data);  
        $this->load->view('website/default/footer');   

    }
    
    function password_recover()
    {
		if (session_check()) {
            redirect('insider/edit_password', 'refresh');
        }
		
        $this->load->library("form_validation");   
        $data['error'] = '';

        if($this->input->post("recover_submit",TRUE) !== FALSE){
            if(validate_token("recover",$this->input->post("recover_token",TRUE))){
                if($this->_validate_recover_form()){   

                    $this->load->model('usermodel');
                    $email = $this->input->post("recover_email",TRUE);                    

                    $user_id = $this->usermodel->get_user_from_email($email);

                    if($user_id !== FALSE){

                        $this->load->helper("string");
                        $new_password = strtolower(random_string("alnum", 8));

                        $this->usermodel->set_new_password($user_id, sha1($new_password));
                                                             
                        $this->load->helper('send_email');     
                        $template_file = EMAIL_TEMPLATES_FOLDER.'password_recover'.INSTANCE_FILE_SUFFIX.'.html';
                        $template_data = array("EMAIL" => $email,
                                                "PASSWORD" => $new_password);

                        send_email(EMAIL_SENDER, 
                                    EMAIL_SENDER_NAME, 
                                    $email, 
                                    $this->lang->line('Password_recover_subject'),   
                                    $template_file,
                                    $template_data);        
                        
                        $password_recover_success = TRUE;                                 

                    } else {

                        $data['error'] = '<p class="error">'.$this->lang->line("PassRecoverInvalidEmail").'</p>';

                    }


                }
            } 
        }

        $data['token'] = generate_token("recover",10);

        $this->load->view('website/default/head', array("document_title" => DOCUMENT_TITLE . $this->lang->line('Members_Program')." - Password recover")); 
        $this->load->view('website/default/header');

        if (isset($password_recover_success))
            $this->load->view('website/insider/password_recover_success'); 
        else 
            $this->load->view('website/insider/password_recover', $data);  

        $this->load->view('website/default/footer');   


    }

    private function _validate_recover_form(){  
          
        $config = array(
                        array(
                         'field'   => 'recover_email',
                         'label'   => 'Email',
                         'rules'   => 'trim|required|xss_clean|valid_email'
                        )
                    );

        
        $this->form_validation->set_rules($config);       
        $this->form_validation->set_error_delimiters('<p class="error">', '</p>');
        
        if ($this->form_validation->run($this) == FALSE){
            return FALSE;
        }else
            return TRUE;

    }
	
/*
    public function _check_duplicated_email($email){ 

		$this->load->library("form_validation"); 

		$this->form_validation->set_message('check_duplicated_email', $this->lang->line('EmailAlreadyExists'));
		return FALSE;

        if($this->usermodel->is_duplicated('Username_Users', $email)){

            $this->form_validation->set_message('check_duplicated_email', $this->lang->line('EmailAlreadyExists'));
            return FALSE;

        }else{

            return TRUE;    

        }
        
    } 	
*/	
     
}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */